var colors = require('colors');
const { exec } = require('child_process');

var Base = require('./base');
String.prototype.format = require('./base').strFormat;
var redis = Base.openRedis('redis读取');

globalConfig = {
    redisSaveLogLimitSum: 110
};

var node_url = process.argv[2];

Base.initList[0] = function (done) {
    console.log('启动中[0]...');
    console.log('检测EOS区块链连接...');
    Base.checkDockerEOS(node_url, (isCleos, isDocker) => {
        console.log('checkDockerEOS');
        if (isCleos || isDocker) {
            cleosExe = Base.cleosExe;
            done();
            return;
        }
        else {
            done(true, 'can not find cleos');
            return;
        }
    });
}

function CheckSum() {
    redis.llen('dice1betlog', (err, res) => {
        if (err) { console.log(err.red); return; }
        if (parseInt(res) > globalConfig.redisSaveLogLimitSum) {
            for (var i = 0; i < parseInt(res) - globalConfig.redisSaveLogLimitSum; i++) {
                if (i === 50) {
                    return;
                }
                redis.lpop('dice1betlog', (err, res) => {
                    if (err) { console.log(err.red); return; }
                });
            }
        }
    });
}
//setInterval(CheckSum, 2000);

function getList1() {
    var cmd = cleosExe + ' --url ' + node_url + ' get table fastwingifts fastwingifts giftlist1 -l 200';

    var cmds = []
    cmds.push(cmd);
    cmd = cleosExe + ' --url ' + node_url + ' get table fastwingifts fastwingifts giftlist2 -l 200';
    cmds.push(cmd);
    cmd = cleosExe + ' --url ' + node_url + ' get table fastwingifts fastwingifts giftlist3 -l 200';
    cmds.push(cmd);
    cmd = cleosExe + ' --url ' + node_url + ' get table fastwingifts fastwingifts giftlist4 -l 200';
    cmds.push(cmd);

    for (var i = 0;i<cmds.length;i++)
    {
        cmd = cmds[i];
        exec(cmd, (err, stdout, stderr) => {
            if (err) { console.log(err); }
            if (stderr) { console.log(stderr); }
            if (stdout) { console.log(stdout); }

            var jsonObj;
            try {
                jsonObj = JSON.parse(stdout);
            } catch (e) {
                jsonObj = {};
                try{
                    jsonObj = JSON.parse(stdout + '}],"more": true}');
                }catch (ee)
                {
                    setTimeout(getList1, 1000);
                    return;
                }
            }

            for (var data in jsonObj.rows) {
                var line = jsonObj.rows[data];
                redis.sadd('gifts-getlist1', JSON.stringify(line), (err, res) => { 
                    
                });
            }
            
        });
    }
    setTimeout(getList1, 1000);
    
}

function getList2() {
    var cmd = cleosExe + ' --url ' + node_url + ' get table fastwingifts fastwingifts giftlist2 -l 200';
    exec(cmd, (err, stdout, stderr) => {
        if (err) { console.log(err); }
        if (stderr) { console.log(stderr); }
        if (stdout) { console.log(stdout); }

        var jsonObj;
        try {
            jsonObj = JSON.parse(stdout);
        } catch (e) {
            try{
                jsonObj = JSON.parse(stdout + '}],"more": true}');
            }catch (ee)
            {
                setInterval(getList2, 1000);
                return;
            }
            
        }

        for (var data in jsonObj.rows) {
            var line = jsonObj.rows[data];
            redis.sadd('gifts-getlist2', JSON.stringify(line), (err, res) => { });
        }
        setInterval(getList2, 1000);
    });
}

function getList3() {
    var cmd = cleosExe + ' --url ' + node_url + ' get table fastwingifts fastwingifts giftlist3 -l 200';
    exec(cmd, (err, stdout, stderr) => {
        if (err) { console.log(err); }
        if (stderr) { console.log(stderr); }
        if (stdout) { console.log(stdout); }

        var jsonObj;
        try {
            jsonObj = JSON.parse(stdout);
        } catch (e) {
            try{
                jsonObj = JSON.parse(stdout + '}],"more": true}');
            }catch (ee)
            {
                setInterval(getList3, 1000);
                return;
            }
        }

        for (var data in jsonObj.rows) {
            var line = jsonObj.rows[data];
            redis.sadd('gifts-getlist3', JSON.stringify(line), (err, res) => { });
        }
        setInterval(getList3, 1000);
    });
}

function getList4() {
    var cmd = cleosExe + ' --url ' + node_url + ' get table fastwingifts fastwingifts giftlist4 -l 200';
    exec(cmd, (err, stdout, stderr) => {
        if (err) { console.log(err); }
        if (stderr) { console.log(stderr); }
        if (stdout) { console.log(stdout); }

        var jsonObj;
        try {
            jsonObj = JSON.parse(stdout);
        } catch (e) {
            try{
                jsonObj = JSON.parse(stdout + '}],"more": true}');
            }catch (ee)
            {
                setInterval(getList4, 1000);
                return;
            }
        }

        for (var data in jsonObj.rows) {
            var line = jsonObj.rows[data];
            redis.sadd('gifts-getlist4', JSON.stringify(line), (err, res) => { });
        }
        setInterval(getList4, 1000);
    });
}


Base.initList[1] = function (done) {
    getList1();
    /*getList2();
    getList3();
    getList4();*/
    done();
}

Base.begin();

